
package study.Stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;

public class EndMethod {
    public EndMethod() {
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList();
        Collections.addAll(list, new String[]{"张三", "王五", "李明", "张凯", "丽君", "张国荣"});
        System.out.println("========================forEach");
        list.stream().forEach((s) -> System.out.println(s));
        System.out.println("========================count");
        long count = list.stream().count();
        System.out.println(count);
        System.out.println("========================toArray");
        Object[] arr1 = list.stream().toArray();
        System.out.println(Arrays.toString(arr1));
        System.out.println("-------------------------------");
        String[] arr2 = (String[])list.stream().toArray(new IntFunction<String[]>() {
            public String[] apply(int value) {
                return new String[value];
            }
        });
        System.out.println(Arrays.toString(arr2));
        System.out.println("-------------------------------");
        String[] arr3 = (String[])list.stream().toArray((value) -> new String[value]);
        System.out.println(Arrays.toString(arr3));
        List<Integer> intList = new ArrayList();
        Collections.addAll(intList, new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9});
        int[] ints = intList.stream().mapToInt((i) -> i).toArray();
        long[] longs = intList.stream().mapToLong((i) -> (long)i).toArray();
        System.out.println("========================collect");
        List<String> list2 = new ArrayList();
        Collections.addAll(list2, new String[]{"张三-1", "王五-2", "李明-3", "张凯-4", "丽君-5", "张国荣-6"});
        List<String> collectList = (List)list2.stream().filter((s) -> s.split("-")[0].startsWith("张")).collect(Collectors.toList());
        System.out.println(collectList);
        Set<String> collectSet = (Set)list2.stream().filter((s) -> s.split("-")[0].startsWith("张")).collect(Collectors.toSet());
        System.out.println(collectList);
        Map<String, Integer> collectMap = (Map)list2.stream().filter((s) -> s.split("-")[0].startsWith("张")).collect(Collectors.toMap(new Function<String, String>() {
            public String apply(String string) {
                return string.split("-")[0];
            }
        }, new Function<String, Integer>() {
            public Integer apply(String string) {
                return Integer.parseInt(string.split("-")[1]);
            }
        }));
        Map<String, Integer> collectMap2 = (Map)list2.stream().filter((s) -> s.split("-")[0].startsWith("张")).collect(Collectors.toMap((s) -> s.split("-")[0], (s) -> Integer.parseInt(s.split("-")[1])));
        System.out.println(collectMap);
        System.out.println(collectMap2);
    }
}
